// pages/answer/answer.js
//获取应用实例
var app = getApp();
var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tradeNo: '',
    radioInfo: null,
    topicRecordInfo: null,
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    var that = this;
    app.getUserInfo(function (res) {
      that.setData({
        userInfo: res
      });
    });
    this.setData({
      tradeNo: options.tradeNo
    });
    this.getHbTheTopicRecordInfo();
  },

  getHbTheTopicRecordInfo: function () {
    debugger
    var that = this;
    var method = route.getHbTheTopicRecordInfo();

    var params = {
      tradeNo: that.data.tradeNo,
      userId: app.globalData.userInfo.id
    };
    httpClient.request(method, 'GET', params, function (res, err) {
      debugger
      var reciverStatus = res.reciverStatus;
      if (reciverStatus != 0) {
        wx.redirectTo({
          url: '../creatpacket/creatpacket?tradeNo=' + that.data.tradeNo,
        })
        return;
      }
      var questions = res.hbQuestions;
      that.setData({
        avatarUrl: res.imgUrl,
        name: res.name,
        topicRecordInfo: res,
        answerRecordList: res.answerRecordList
      });
      if (questions.questionsType == 0) {
        that.setData({
          tab1: 'none',
          tab2: 'block',
          radioInfo: res
        });
      } else {
        that.setData({
          tab1: 'block',
          tab2: 'none',
          crosswordInfo: res
        });
      }
      var data = res.hbQuestions.answer;
      var _TheArray = new Array();
      for (var i = 0, len = data.length; i < len; i++) {
        _TheArray[i] = { message: '' };
      }
      that.setData({
        array: _TheArray,
      });

      if (res.statusRecord != 0) {
        var strArray = that.data.array;
        var data = res.hbQuestions.answer;
        for (var i = 0; i < strArray.length; i++) {
          var value = data.charAt(i);
          strArray[i].message = value;
        }
        that.setData({
          array: strArray,
        });
      }

    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    return {
      title: '妙趣派',
      desc: '最具人气的答题红包!',
      path: '/pages/guesspacket/guesspacket?tradeNo=' + that.data.tradeNo,
      success: function (res) {
        // 转发成功
        wx.navigateTo({
          url: '../guesspacket/guesspacket?tradeNo=' + that.data.tradeNo
          // url: '/pages/guesspacket/guesspacket'
        })
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  }
})